/*
 *   日志输出
 */

#include "basic/trace.h"
#include "basic/utilitys.h"

#include <sys/syscall.h>
#include <unistd.h>

#include <algorithm>
#include <cstdarg>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <string>

namespace wheels {

static const char *s_color[] = {
    "\e[30m", "\e[31m", "\e[32m", "\e[33m", "\e[34m", "\e[35m", "\e[36m", "\e[37m",
};

// inline long getselfThrID() { return syscall(SYS_gettid); }

int32_t trace_logger(FILE *stm, color c, const char *branch, const char *ver, const char *file, const char *func,
                     uint32_t line, const char *fmt, ...) {
    std::ostringstream format;
    format << s_color[c];
    format << "[" << branch << ":" << ver << "] ";
    auto currTm = time(nullptr);
    auto tm = localtime(&currTm);
    format << std::setfill('0') << std::setw(2) << tm->tm_hour << ":" << std::setw(2) << tm->tm_min << ":"
           << std::setw(2) << tm->tm_sec << " ";
    // format << "[" << getselfThrID() << " | ";
    format << file << " | " << func << " | " << line << "] ";
    format << fmt << "\e[m";

    va_list ap;
    va_start(ap, fmt);
    auto ret = vfprintf(stm, format.str().c_str(), ap);
    va_end(ap);

    return ret;
}

}  // namespace wheels
